package com.ai.dmc.index.web.agent;


import com.ai.dmc.index.web.agent.utils.BaiduAgentUtils;
//import com.pansky.integration.common.utils.CacheUtils;
import com.ai.dmc.index.web.agent.utils.HttpUtils;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/baiduAgent"})
public class BaiduMapAgentController
{
  private HashMap<String, String> getHm(HttpServletRequest request)
  {
    String agentUtrl = (request.getRequestURL().toString().replace(request.getRequestURI(), "") + request.getContextPath()).replace("http://", "");
    agentUtrl = agentUtrl + "/baidu/agent";

    HashMap hm = new HashMap();
    hm.put("online4.map.bdimg.com", agentUtrl + "/online/4");
    hm.put("online3.map.bdimg.com", agentUtrl + "/online/3");
    hm.put("online2.map.bdimg.com", agentUtrl + "/online/2");
    hm.put("online1.map.bdimg.com", agentUtrl + "/online/1");
    hm.put("online0.map.bdimg.com", agentUtrl + "/online/0");
    hm.put("api.map.bdimg.com", agentUtrl + "/api");
    hm.put("api0.map.bdimg.com", agentUtrl + "/api/0");
    hm.put("api1.map.bdimg.com", agentUtrl + "/api/1");
    hm.put("api2.map.bdimg.com", agentUtrl + "/api/2");
    hm.put("api.map.baidu.com", agentUtrl + "/baidu");

    return hm;
  }
  @RequestMapping({"agent/baidu"})
  public void apiBaidu(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
    String url = "http://api.map.baidu.com/";
    String resultJs = HttpUtils.sendGet(url, request.getQueryString());
    response.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();
    out.print(resultJs);
  }
  @RequestMapping({"agent/baidu/{a}/{b}"})
  public void apiBaidu(@PathVariable("a") String a, @PathVariable("b") String b, HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
    String servletPath = request.getServletPath();
    String url = "http://api.map.baidu.com/" + a + "/" + b + BaiduAgentUtils.getImageSuffix(servletPath);
    if (a.equals("images")) {
      HttpUtils.getImage(url, request, response);
    } else {
      String resultJs = HttpUtils.sendGet(url, request.getQueryString());
      response.setCharacterEncoding("UTF-8");
      PrintWriter out = response.getWriter();
      out.print(resultJs);
    }
  }

  @RequestMapping({"agent/api"})
  public void api(HttpServletRequest request, HttpServletResponse response, Model model)
    throws IOException
  {
    String url = "http://api.map.bdimg.com/";
    String resultJs = HttpUtils.sendGet(url, request.getQueryString());
    response.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();
    out.print(resultJs);
  }

  @RequestMapping({"agent/api/{serverType}/{a}"})
  public void api(@PathVariable("serverType") String serverType, @PathVariable("a") String a, HttpServletRequest request, HttpServletResponse response, Model model)
    throws IOException
  {
    String servletPath = request.getServletPath();
    String url = "http://api" + serverType + ".map.bdimg.com/" + a + BaiduAgentUtils.getImageSuffix(servletPath);
    if (a.equals("images")) {
      HttpUtils.getImage(url, request, response);
    } else {
      String resultJs = HttpUtils.sendGet(url, request.getQueryString());
      response.setCharacterEncoding("UTF-8");
      PrintWriter out = response.getWriter();
      out.print(resultJs);
    }
  }

  @RequestMapping({"agent/api/{serverType}/{a}/{b}"})
  public void api(@PathVariable("serverType") String serverType, @PathVariable("a") String a, @PathVariable("b") String b, HttpServletRequest request, HttpServletResponse response, Model model)
    throws IOException
  {
    String servletPath = request.getServletPath();
    String url = "http://api" + serverType + ".map.bdimg.com/" + a + "/" + b + BaiduAgentUtils.getImageSuffix(servletPath);
    if (a.equals("images")) {
      HttpUtils.getImage(url, request, response);
    } else {
      String resultJs = HttpUtils.sendGet(url, request.getQueryString());
      response.setCharacterEncoding("UTF-8");
      PrintWriter out = response.getWriter();
      out.print(resultJs);
    }
  }

  @RequestMapping({"agent/online/{serverType}/{ywlx}"})
  public void online(@PathVariable("serverType") String serverType, @PathVariable("ywlx") String ywlx, HttpServletRequest request, HttpServletResponse response, Model model)
    throws IOException
  {
    if (StringUtils.isNotBlank(ywlx)) {
      String url = "http://online" + serverType + ".map.bdimg.com/" + ywlx + "/";
      if (ywlx.equals("js")) {
        String resultJs = HttpUtils.sendGet(url, request.getQueryString());
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.print(resultJs);
      } else if (ywlx.equals("tile")) {
        HttpUtils.getImage(url, request, response); } 
    }
  }

  @RequestMapping({"getscript"})
  @ResponseBody
  public String getscript(HttpServletRequest request, HttpServletResponse response, Model model) { String cache = request.getParameter("cache");
//    if ((StringUtils.isNotBlank(cache)) && (CacheUtils.get("script") != null) && (((String)CacheUtils.get("script")).length() > 2)) {
//      return (String)CacheUtils.get("script");
//    }

    String url = request.getParameter("agent");

    String content = HttpUtils.sendGet(url, "");
    Object[] rs = getHm(request).keySet().toArray();
    for (Object s : rs) {
      content = content.replaceAll((String)s, (String)getHm(request).get(s));
    }
//    CacheUtils.put("script", content);
    return content; } 
  @RequestMapping({"refreshScript"})
  @ResponseBody
  public String refreshScript(HttpServletRequest request, HttpServletResponse response, Model model) {
//    CacheUtils.put("script", null);
    return "success";
  }
}